﻿using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace blackhouse.WebNemo.Web {

    [WebService(Namespace = "http://nemo.blackhouse.pl/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class NemoService : System.Web.Services.WebService {

        [WebMethod]
        public bool SaveFile(int groupId, string column, int recordId, string fileName, byte[] fileData, int userId) {
            GroupRecord record = new GroupRecord(groupId, recordId);
            if (!record.Exists) return false;
            if (!(record.LastModifiedBy == userId || record.CreatedBy == userId)) return false;
            using (MemoryStream ms = new MemoryStream(fileData)) {
                record.SetFileValue(column, fileName, ms);
            }
            record.Save(userId);
            return true;
        }
    }
}
